package cn.com.cfae.iras.doc.analyze.handUtil;

import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.redis.RedisUtil;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class HandingData {
    private static final Logger logger = LoggerFactory.getLogger(HandingData.class);

    public Map<String,Object> isContainsKeyWord(JsonElement jsonElement, String keyWord){
        Map<String, Object> reslt = new HashMap<>();

        List<ComponentResult.TextPosition> textPositions = new ArrayList<>();
        ComponentResult.TextPosition contentAndPosition =null;

        JsonArray jsonArray = null;
        if (jsonElement.isJsonArray()){
            jsonArray = jsonElement.getAsJsonArray();
        }
        boolean flag = false;
        Double objX = 0.0;
        Double objY = 0.0;
        if (!jsonArray.isJsonNull()){
            for (int i=0;i<jsonArray.size();i++){
                contentAndPosition = new ComponentResult().new TextPosition();

                JsonObject asJsonObject = jsonArray.get(i).getAsJsonObject();
                Set set = asJsonObject.entrySet();
                Iterator<Map.Entry<String, JsonElement>> it = set.iterator();
                String key ;

                while (it.hasNext()){
                    Map.Entry<String, JsonElement> entry = it.next();
                    key= entry.getKey();
                    if (StringUtils.isNotEmpty(key)&&key.equals("top")){
                      objY =  Double.parseDouble(entry.getValue().getAsString());
                    }
                    if (StringUtils.isNotEmpty(key)&&key.equals("left")){
                       objX = Double.parseDouble(entry.getValue().getAsString());
                    }
                    if (StringUtils.isNotEmpty(key)&&key.equals("data")){
                        JsonArray dataJsonArray = null;
                        if (entry.getValue().isJsonArray()){
                            dataJsonArray  = entry.getValue().getAsJsonArray();
                        }
                        if (!dataJsonArray.isJsonNull()){
                            for (int d=0; d<dataJsonArray.size(); d++){
                                JsonArray delmJsonArray = dataJsonArray.get(d).getAsJsonArray();
                                if (!dataJsonArray.isJsonNull()){
                                    for (int de=0;de<delmJsonArray.size();de++){
                                        JsonObject deJsonObject = delmJsonArray.get(de).getAsJsonObject();
                                        Set deSet = deJsonObject.entrySet();
                                        Iterator<Map.Entry<String, JsonElement>> deIt = deSet.iterator();
                                        Double x = 0.0;
                                        Double y = 0.0;
                                        while (deIt.hasNext()){
                                            Map.Entry<String, JsonElement> deEntry = deIt.next();
                                           String deKey= deEntry.getKey();
                                            if (StringUtils.isNotEmpty(deKey)&&deKey.equals("top")){
                                                y = Double.parseDouble(deEntry.getValue().getAsString());
                                            }
                                            if (StringUtils.isNotEmpty(deKey)&&deKey.equals("left")){
                                                x =  Double.parseDouble(deEntry.getValue().getAsString());
                                            }
                                            if (StringUtils.isNotEmpty(deKey)&&deKey.equals("text")){
                                                String count = deEntry.getValue().getAsString();
                                                if (StringUtils.isNotEmpty(count)){
                                                    if (count.contains(keyWord)){
                                                        contentAndPosition.setX(x);
                                                        contentAndPosition.setY(y);
                                                        contentAndPosition.setContent(count);
                                                        textPositions.add(contentAndPosition);
                                                        flag = true;
                                                        reslt.put("flag",flag);
                                                        reslt.put("position",textPositions);
                                                        break;
                                                    }
                                                }

                                           }
                                        }
                                        if (flag){
                                            break;
                                        }
                                    }
                                }
                                if (flag){
                                    break;
                                }
                            }
                        }
                    }
                    if (flag){
                        break;
                    }

                }
                if (!flag){
                    contentAndPosition.setX(objX);
                    contentAndPosition.setY(objY);
                    contentAndPosition.setContent(keyWord);
                    textPositions.add(contentAndPosition);
                    reslt.put("flag",flag);
                    reslt.put("position",textPositions);
                }
                if (flag)
                    break;
            }


        }

        return reslt;
    }


    public Map<String,Object> isContainsKeyWordOfRegex(JsonElement jsonElement, String regex){
        Map<String, Object> reslt = new HashMap<>();

        List<ComponentResult.TextPosition> textPositions = new ArrayList<>();
        ComponentResult.TextPosition contentAndPosition =null;

        JsonArray jsonArray = null;
        if (jsonElement.isJsonArray()){
            jsonArray = jsonElement.getAsJsonArray();
        }
        boolean flag = false;
        Double objX = 0.0;
        Double objY = 0.0;
        Pattern pattern = null;
        Matcher matcher = null;
        if (!jsonArray.isJsonNull()){
            for (int i=0;i<jsonArray.size();i++){
                contentAndPosition = new ComponentResult().new TextPosition();

                JsonObject asJsonObject = jsonArray.get(i).getAsJsonObject();
                Set set = asJsonObject.entrySet();
                Iterator<Map.Entry<String, JsonElement>> it = set.iterator();
                String key ;

                while (it.hasNext()){
                    Map.Entry<String, JsonElement> entry = it.next();
                    key= entry.getKey();
                    if (StringUtils.isNotEmpty(key)&&key.equals("top")){
                        objY =  Double.parseDouble(entry.getValue().getAsString());
                    }
                    if (StringUtils.isNotEmpty(key)&&key.equals("left")){
                        objX = Double.parseDouble(entry.getValue().getAsString());
                    }
                    if (StringUtils.isNotEmpty(key)&&key.equals("data")){//当前章内容
                        JsonArray dataJsonArray = null;
                        if (entry.getValue().isJsonArray()){
                            dataJsonArray  = entry.getValue().getAsJsonArray();
                        }
                        if (!dataJsonArray.isJsonNull()){
                            for (int d=0; d<dataJsonArray.size(); d++){//当前章循环行
                                JsonArray delmJsonArray = dataJsonArray.get(d).getAsJsonArray();
                                if (!dataJsonArray.isJsonNull()){
                                    for (int de=0;de<delmJsonArray.size();de++){
                                        JsonObject deJsonObject = delmJsonArray.get(de).getAsJsonObject();//当前行
                                        Set deSet = deJsonObject.entrySet();
                                        Iterator<Map.Entry<String, JsonElement>> deIt = deSet.iterator();
                                        Double x = 0.0;
                                        Double y = 0.0;
                                        while (deIt.hasNext()){
                                            Map.Entry<String, JsonElement> deEntry = deIt.next();
                                            String deKey= deEntry.getKey();
                                            if (StringUtils.isNotEmpty(deKey)&&deKey.equals("top")){
                                                y = Double.parseDouble(deEntry.getValue().getAsString());
                                            }
                                            if (StringUtils.isNotEmpty(deKey)&&deKey.equals("left")){
                                                x =  Double.parseDouble(deEntry.getValue().getAsString());
                                            }
                                            if (StringUtils.isNotEmpty(deKey)&&deKey.equals("text")){

                                                String count = deEntry.getValue().getAsString();
                                                if (StringUtils.isNotEmpty(count)){
                                                    pattern = Pattern.compile(regex);
                                                    matcher = pattern.matcher(count);
                                                    if (matcher.matches()){
                                                        contentAndPosition.setX(x);
                                                        contentAndPosition.setY(y);
                                                        contentAndPosition.setContent(count);
                                                        textPositions.add(contentAndPosition);
                                                        flag = true;
                                                        reslt.put("flag",flag);
                                                        reslt.put("position",textPositions);
                                                        break;
                                                    }
                                                }

                                            }
                                        }
                                        if (flag){
                                            break;
                                        }
                                    }
                                }
                                if (flag){
                                    break;
                                }
                            }
                        }
                    }
                    if (flag){
                        break;
                    }

                }
                if (!flag){
                    contentAndPosition.setX(objX);
                    contentAndPosition.setY(objY);
                    contentAndPosition.setContent("");
                    textPositions.add(contentAndPosition);
                    reslt.put("flag",flag);
                    reslt.put("position",textPositions);
                }
                if (flag)
                    break;
            }


        }

        return reslt;
    }


    public Map<String,Object> isContainsKeywords2LiensOfSynonyms(JsonElement jsonElement, String synonyms){
        Map<String, Object> reslt = new HashMap<>();
        List<String> keywordList = new ArrayList<>();
        if (StringUtils.isNotEmpty(synonyms)){
            if (synonyms.contains(",")){
                String[]  keywords = synonyms.split(",");
                for (String keyword:keywords){
                    keywordList.add(keyword);
                }
            }else {
                keywordList.add(synonyms);
            }
        }

        List<ComponentResult.TextPosition> textPositions = new ArrayList<>();
        ComponentResult.TextPosition contentAndPosition =null;

        JsonArray jsonArray = null;
        if (jsonElement.isJsonArray()){
            jsonArray = jsonElement.getAsJsonArray();
        }
        boolean flag = false;
        Double objX = 0.0;
        Double objY = 0.0;
        Pattern pattern = null;
        Matcher matcher = null;
        if (!jsonArray.isJsonNull()){
            for (int i=0;i<jsonArray.size();i++){

                JsonObject asJsonObject = jsonArray.get(i).getAsJsonObject();
                Set set = asJsonObject.entrySet();
                Iterator<Map.Entry<String, JsonElement>> it = set.iterator();
                String key ;

                while (it.hasNext()){
                    Map.Entry<String, JsonElement> entry = it.next();
                    key= entry.getKey();
                    if (StringUtils.isNotEmpty(key)&&key.equals("top")){
                        objY =  Double.parseDouble(entry.getValue().getAsString());
                    }
                    if (StringUtils.isNotEmpty(key)&&key.equals("left")){
                        objX = Double.parseDouble(entry.getValue().getAsString());
                    }
                    if (StringUtils.isNotEmpty(key)&&key.equals("data")){//当前章内容
                        JsonArray dataJsonArray = null;
                        if (entry.getValue().isJsonArray()){
                            dataJsonArray  = entry.getValue().getAsJsonArray();
                        }
                        if (!dataJsonArray.isJsonNull()){
                            for (int d=0; d<dataJsonArray.size(); d++){//当前章循环行
                                JsonArray delmJsonArray = dataJsonArray.get(d).getAsJsonArray();
                                if (!dataJsonArray.isJsonNull()){
                                    for (int de=0;de<delmJsonArray.size();de++){
                                        JsonObject deJsonObject = delmJsonArray.get(de).getAsJsonObject();//当前行
                                        Set deSet = deJsonObject.entrySet();
                                        Iterator<Map.Entry<String, JsonElement>> deIt = deSet.iterator();
                                        Double x = 0.0;
                                        Double y = 0.0;
                                        while (deIt.hasNext()){
                                            Map.Entry<String, JsonElement> deEntry = deIt.next();
                                            String deKey= deEntry.getKey();
                                            if (StringUtils.isNotEmpty(deKey)&&deKey.equals("top")){
                                                y = Double.parseDouble(deEntry.getValue().getAsString());
                                            }
                                            if (StringUtils.isNotEmpty(deKey)&&deKey.equals("left")){
                                                x =  Double.parseDouble(deEntry.getValue().getAsString());
                                            }
                                            if (StringUtils.isNotEmpty(deKey)&&deKey.equals("text")){
                                                boolean isExist = false;
                                                String count = deEntry.getValue().getAsString();

                                                if (StringUtils.isNotEmpty(count)){

                                                    ComponentResult.TextPosition frontTextPostion = getFrontString(dataJsonArray, d);


                                                    //当前行

                                                    //后一行
                                                    ComponentResult.TextPosition behindTextPostion = getBehindString(dataJsonArray, d);

                                                    if (CollectionUtils.isNotEmpty(keywordList)){
                                                        for (String keyword :keywordList){
                                                            //判断是否在当前行

                                                            if (!isExist&&count.contains(keyword)){
                                                                contentAndPosition = new ComponentResult().new TextPosition();
                                                                flag = true;
                                                                isExist = true;
                                                                int countBegenIndex = count.indexOf(keyword)+1;
                                                                int countEndIndex = countBegenIndex+keyword.length()-1;
                                                                contentAndPosition.setX(x);
                                                                contentAndPosition.setY(y);
                                                                contentAndPosition.setContent(count.substring(countBegenIndex-1,countEndIndex));
                                                                textPositions.add(contentAndPosition);
                                                                reslt.put("flag",flag);
                                                                reslt.put("position",textPositions);
                                                                break;
                                                            }

                                                            //判断是否在后一行
                                                            if (!isExist&&StringUtils.isNotEmpty(behindTextPostion.getContent())){
                                                                String behindText = behindTextPostion.getContent();
                                                                if (behindText.contains(keyword)){
                                                                    contentAndPosition = new ComponentResult().new TextPosition();
                                                                    flag = true;
                                                                    isExist = true;
                                                                    int countBegenIndex = behindText.indexOf(keyword)+1;
                                                                    int countEndIndex = countBegenIndex+keyword.length()-1;
                                                                    contentAndPosition.setX(behindTextPostion.getX());
                                                                    contentAndPosition.setY(behindTextPostion.getY());
                                                                    contentAndPosition.setContent(behindText.substring(countBegenIndex-1,countEndIndex));
                                                                    textPositions.add(contentAndPosition);
                                                                    reslt.put("flag",flag);
                                                                    reslt.put("position",textPositions);
                                                                    break;
                                                                }

                                                            }
                                                            //判断前一行 和当前行
                                                            if (!isExist&&StringUtils.isNotEmpty(frontTextPostion.getContent())){

                                                                //前一行文本
                                                                String fornText = frontTextPostion.getContent();
                                                                //总文本
                                                                String temp = fornText+count;
                                                                if (temp.contains(keyword)){
                                                                    flag = true;
                                                                    isExist = true;
                                                                    //当前关键词的位置
                                                                    int countKeywrodIndex = temp.indexOf(keyword)+1;
                                                                    //关键词长度
                                                                    int keywrodLenth = keyword.length();
                                                                    //关键词结尾位置
                                                                    int keywordIndexEndLenth = keywrodLenth+countKeywrodIndex-1;//删除掉开头
                                                                    //前一行长度
                                                                    int frontLenth = fornText.length();
                                                                    //第二行结束位置
                                                                    int endIndex = keywordIndexEndLenth-countKeywrodIndex-1;

                                                                    if (frontLenth>=countKeywrodIndex){
                                                                        contentAndPosition = new ComponentResult().new TextPosition();
                                                                        contentAndPosition.setX(frontTextPostion.getX());
                                                                        contentAndPosition.setY(frontTextPostion.getY());
                                                                        contentAndPosition.setContent(fornText.substring(countKeywrodIndex-1,frontLenth));
                                                                        textPositions.add(contentAndPosition);
                                                                    }
                                                                    if (endIndex<=count.length()){
                                                                        contentAndPosition = new ComponentResult().new TextPosition();
                                                                        contentAndPosition.setX(x);
                                                                        contentAndPosition.setY(y);
                                                                        contentAndPosition.setContent(count.substring(0,endIndex));
                                                                        textPositions.add(contentAndPosition);
                                                                    }
                                                                    reslt.put("flag",flag);
                                                                    reslt.put("position",textPositions);
                                                                    break;
                                                                }
                                                            }
                                                            //判断当前行和后一行
                                                            if (!isExist&&StringUtils.isNotEmpty(behindTextPostion.getContent())){
                                                                //后一行的文本
                                                                String behindText = behindTextPostion.getContent();
                                                                //总文本
                                                                String temp = count+behindText;

                                                                if (temp.contains(keyword)){
                                                                    flag = true;
                                                                    isExist = true;

                                                                    //当前关键词的位置
                                                                    int countKeywrodIndex = temp.indexOf(keyword)+1;
                                                                    //关键词长度
                                                                    int keywrodLenth = keyword.length();
                                                                    //关键词结尾位置
                                                                    int keywordIndexEndLenth = keywrodLenth+countKeywrodIndex-1;//删除掉开头
                                                                    //当前行长度
                                                                    int coumtLenth = count.length();
                                                                    //第二行结束位置
                                                                    int endIndex = keywordIndexEndLenth-coumtLenth;

                                                                    if (coumtLenth>=countKeywrodIndex){
                                                                        contentAndPosition = new ComponentResult().new TextPosition();
                                                                        contentAndPosition.setX(x);
                                                                        contentAndPosition.setY(y);
                                                                        contentAndPosition.setContent(count.substring(countKeywrodIndex-1,coumtLenth));
                                                                        textPositions.add(contentAndPosition);
                                                                    }
                                                                    if (endIndex<=behindText.length()){
                                                                        contentAndPosition = new ComponentResult().new TextPosition();
                                                                        contentAndPosition.setX(behindTextPostion.getX());
                                                                        contentAndPosition.setY(behindTextPostion.getY());
                                                                        contentAndPosition.setContent(behindText.substring(0,endIndex));
                                                                        textPositions.add(contentAndPosition);
                                                                    }

                                                                    reslt.put("flag",flag);
                                                                    reslt.put("position",textPositions);
                                                                    break;

                                                                }

                                                            }
                                                        }
                                                    }


                                                }

                                            }
                                        }
                                        if (flag){
                                            break;
                                        }
                                    }
                                }
                                if (flag){
                                    break;
                                }
                            }
                        }
                    }
                    if (flag){
                        break;
                    }

                }
                if (!flag){
                    contentAndPosition = new ComponentResult().new TextPosition();
                    contentAndPosition.setX(objX);
                    contentAndPosition.setY(objY);
                    contentAndPosition.setContent("");
                    textPositions.add(contentAndPosition);
                    reslt.put("flag",flag);
                    reslt.put("position",textPositions);
                }
                if (flag)
                    break;
            }


        }

        return reslt;
    }

    public Map<String,Object> isContainsKeyWord2Lines(JsonElement jsonElement, String keyword){
        Map<String, Object> reslt = new HashMap<>();

        List<ComponentResult.TextPosition> textPositions = new ArrayList<>();
        ComponentResult.TextPosition contentAndPosition =null;

        JsonArray jsonArray = null;
        if (jsonElement.isJsonArray()){
            jsonArray = jsonElement.getAsJsonArray();
        }
        boolean flag = false;
        Double objX = 0.0;
        Double objY = 0.0;
        Pattern pattern = null;
        Matcher matcher = null;
        if (!jsonArray.isJsonNull()){
            for (int i=0;i<jsonArray.size();i++){


                JsonObject asJsonObject = jsonArray.get(i).getAsJsonObject();
                Set set = asJsonObject.entrySet();
                Iterator<Map.Entry<String, JsonElement>> it = set.iterator();
                String key ;

                while (it.hasNext()){
                    Map.Entry<String, JsonElement> entry = it.next();
                    key= entry.getKey();
                    if (StringUtils.isNotEmpty(key)&&key.equals("top")){
                        objY =  Double.parseDouble(entry.getValue().getAsString());
                    }
                    if (StringUtils.isNotEmpty(key)&&key.equals("left")){
                        objX = Double.parseDouble(entry.getValue().getAsString());
                    }
                    if (StringUtils.isNotEmpty(key)&&key.equals("data")){//当前章内容
                        JsonArray dataJsonArray = null;
                        if (entry.getValue().isJsonArray()){
                            dataJsonArray  = entry.getValue().getAsJsonArray();
                        }
                        if (!dataJsonArray.isJsonNull()){
                            for (int d=0; d<dataJsonArray.size(); d++){//当前章循环行
                                JsonArray delmJsonArray = dataJsonArray.get(d).getAsJsonArray();
                                if (!dataJsonArray.isJsonNull()){
                                    for (int de=0;de<delmJsonArray.size();de++){
                                        JsonObject deJsonObject = delmJsonArray.get(de).getAsJsonObject();//当前行
                                        Set deSet = deJsonObject.entrySet();
                                        Iterator<Map.Entry<String, JsonElement>> deIt = deSet.iterator();
                                        Double x = 0.0;
                                        Double y = 0.0;
                                        while (deIt.hasNext()){
                                            Map.Entry<String, JsonElement> deEntry = deIt.next();
                                            String deKey= deEntry.getKey();
                                            if (StringUtils.isNotEmpty(deKey)&&deKey.equals("top")){
                                                y = Double.parseDouble(deEntry.getValue().getAsString());
                                            }
                                            if (StringUtils.isNotEmpty(deKey)&&deKey.equals("left")){
                                                x =  Double.parseDouble(deEntry.getValue().getAsString());
                                            }
                                            if (StringUtils.isNotEmpty(deKey)&&deKey.equals("text")){
                                                boolean isExist = false;
                                                String count = deEntry.getValue().getAsString();
                                                if (StringUtils.isNotEmpty(count)){
                                                    //logger.info("kkkk-------{}",keyword);
                                                    //前一行
                                                    ComponentResult.TextPosition frontTextPostion = getFrontString(dataJsonArray, d);
                                                   // logger.info("q====={}",frontTextPostion.getContent());

                                                    //当前行
                                                   // logger.info("d====={}",count);

                                                    //后一行
                                                    ComponentResult.TextPosition behindTextPostion = getBehindString(dataJsonArray, d);
                                                  // logger.info("h====={}",behindTextPostion.getContent());

                                                    //判断是否在当前行
                                                    if (!isExist&&count.contains(keyword)){
                                                        contentAndPosition = new ComponentResult().new TextPosition();
                                                        flag = true;
                                                        isExist = true;
                                                        int countBegenIndex = count.indexOf(keyword)+1;
                                                        int countEndIndex = countBegenIndex+keyword.length()-1;
                                                        contentAndPosition.setX(x);
                                                        contentAndPosition.setY(y);
                                                        contentAndPosition.setContent(count.substring(countBegenIndex-1,countEndIndex));
                                                        textPositions.add(contentAndPosition);
                                                        reslt.put("flag",flag);
                                                        reslt.put("position",textPositions);
                                                    }

                                                    //判断是否在后一行
                                                    if (!isExist&&StringUtils.isNotEmpty(behindTextPostion.getContent())){
                                                        String behindText = behindTextPostion.getContent();
                                                        if (behindText.contains(keyword)){
                                                            contentAndPosition = new ComponentResult().new TextPosition();
                                                            flag = true;
                                                            isExist = true;
                                                            int countBegenIndex = behindText.indexOf(keyword)+1;
                                                            int countEndIndex = countBegenIndex+keyword.length()-1;
                                                            contentAndPosition.setX(behindTextPostion.getX());
                                                            contentAndPosition.setY(behindTextPostion.getY());
                                                            contentAndPosition.setContent(behindText.substring(countBegenIndex-1,countEndIndex));
                                                            textPositions.add(contentAndPosition);
                                                            reslt.put("flag",flag);
                                                            reslt.put("position",textPositions);
                                                        }
                                                    }
                                                    //判断前一行 和当前行
                                                    if (!isExist&&StringUtils.isNotEmpty(frontTextPostion.getContent())){

                                                        //前一行文本
                                                        String fornText = frontTextPostion.getContent();
                                                        //总文本
                                                        String temp = fornText+count;
                                                        if (temp.contains(keyword)){
                                                            flag = true;
                                                            isExist = true;
                                                            //当前关键词的位置
                                                            int countKeywrodIndex = temp.indexOf(keyword)+1;
                                                            //关键词长度
                                                            int keywrodLenth = keyword.length();
                                                            //关键词结尾位置
                                                            int keywordIndexEndLenth = keywrodLenth+countKeywrodIndex-1;//删除掉开头
                                                            //前一行长度
                                                            int frontLenth = fornText.length();
                                                            //第二行结束位置
                                                            int endIndex = keywordIndexEndLenth-countKeywrodIndex-1;

                                                            if (frontLenth>=countKeywrodIndex){
                                                                contentAndPosition = new ComponentResult().new TextPosition();
                                                                contentAndPosition.setX(frontTextPostion.getX());
                                                                contentAndPosition.setY(frontTextPostion.getY());
                                                                contentAndPosition.setContent(fornText.substring(countKeywrodIndex-1,frontLenth));
                                                                textPositions.add(contentAndPosition);
                                                            }
                                                            if (endIndex<=count.length()){
                                                                contentAndPosition = new ComponentResult().new TextPosition();
                                                                contentAndPosition.setX(x);
                                                                contentAndPosition.setY(y);
                                                                contentAndPosition.setContent(count.substring(0,endIndex));
                                                                textPositions.add(contentAndPosition);
                                                            }
                                                            reslt.put("flag",flag);
                                                            reslt.put("position",textPositions);
                                                        }
                                                    }
                                                    //判断当前行和后一行
                                                    if (!isExist&&StringUtils.isNotEmpty(behindTextPostion.getContent())){
                                                        //后一行的文本
                                                        String behindText = behindTextPostion.getContent();
                                                        //总文本
                                                        String temp = count+behindText;

                                                        if (temp.contains(keyword)){
                                                            flag = true;
                                                            isExist = true;

                                                            //当前关键词的位置
                                                            int countKeywrodIndex = temp.indexOf(keyword)+1;
                                                            //关键词长度
                                                            int keywrodLenth = keyword.length();
                                                            //关键词结尾位置
                                                            int keywordIndexEndLenth = keywrodLenth+countKeywrodIndex-1;//删除掉开头
                                                            //当前行长度
                                                            int coumtLenth = count.length();
                                                            //第二行结束位置
                                                            int endIndex = keywordIndexEndLenth-coumtLenth;

                                                            if (coumtLenth>=countKeywrodIndex){
                                                                contentAndPosition = new ComponentResult().new TextPosition();
                                                                contentAndPosition.setX(x);
                                                                contentAndPosition.setY(y);
                                                                contentAndPosition.setContent(count.substring(countKeywrodIndex-1,coumtLenth));
                                                                textPositions.add(contentAndPosition);
                                                            }
                                                            if (endIndex<=behindText.length()){
                                                                contentAndPosition = new ComponentResult().new TextPosition();
                                                                contentAndPosition.setX(behindTextPostion.getX());
                                                                contentAndPosition.setY(behindTextPostion.getY());
                                                                contentAndPosition.setContent(behindText.substring(0,endIndex));
                                                                textPositions.add(contentAndPosition);
                                                            }

                                                            reslt.put("flag",flag);
                                                            reslt.put("position",textPositions);

                                                        }

                                                    }

                                                }

                                            }
                                        }
                                        if (flag){
                                            break;
                                        }
                                    }
                                }
                                if (flag){
                                    break;
                                }
                            }
                        }
                    }
                    if (flag){
                        break;
                    }

                }
                if (!flag){
                    contentAndPosition = new ComponentResult().new TextPosition();
                    contentAndPosition.setX(objX);
                    contentAndPosition.setY(objY);
                    contentAndPosition.setContent("");
                    textPositions.add(contentAndPosition);
                    reslt.put("flag",flag);
                    reslt.put("position",textPositions);
                }
                if (flag)
                    break;
            }


        }

        return reslt;
    }

    public List<ComponentResult> getDefaultComponentResults(String catalogId,String chapterId,String[] keywords,String prompMsg ,String ruleName,String regulationId,String exist){
        List<ComponentResult> componentResults = new ArrayList<>();
        for (String keyword : keywords){
            ComponentResult componentResult =getComponentResult(catalogId,chapterId,ruleName,keyword,prompMsg,true,regulationId, 0,null,exist);
            componentResults.add(componentResult);
        }
        return componentResults;
    }

    public List<ComponentResult> getDefaultComponentResultsOfKeyWord(String catalogId,String chapterId,String[] keywords,String prompMsg ,Boolean annotationFlag,String ruleName,String regulationId,int pageNum,String exist){
        List<ComponentResult> componentResults = new ArrayList<>();
        for (String keyword : keywords){
            ComponentResult componentResult =getComponentResult(catalogId,chapterId,ruleName,keyword,prompMsg,annotationFlag,regulationId, pageNum,null,exist);
            componentResults.add(componentResult);
        }
        return componentResults;
    }
    public ComponentResult getComponentResult(String catalogId,String chapterId,
                                               String ruleName,String keyWord,
                                               String prompMsg,Boolean annotationFlag,
                                               String regulationId,int pageNum,
                                               List<ComponentResult.TextPosition> textPositions, String exist){
        ComponentResult componentResult = new ComponentResult();
        componentResult.setCatalogId(catalogId);
        componentResult.setChapterId(chapterId);
        componentResult.setRuleName(ruleName);
        componentResult.setUid(UUID.randomUUID().toString());
        componentResult.setKeyword(keyWord);
        componentResult.setPromptMessage(prompMsg);
        componentResult.setAnnotationFlag(annotationFlag);
        componentResult.setRegulationId(regulationId);
        componentResult.setPageNum(pageNum);
        componentResult.setTextPosition(textPositions);
        componentResult.setExist(exist);
        return  componentResult;
    }

    //前一行
    private  ComponentResult.TextPosition  getFrontString(JsonArray dataJsonArray,int row){
        ComponentResult.TextPosition textPosition = new ComponentResult().new TextPosition();
        Double x = 0.0;
        Double y = 0.0;
        boolean flag = false;
        if (dataJsonArray.isJsonNull())
            return textPosition;

        JsonArray delmJsonArray = null;
        if (row!=0){
            delmJsonArray = dataJsonArray.get(row-1).getAsJsonArray();
            if (!delmJsonArray.isJsonNull()){
                for (int de=0;de<delmJsonArray.size();de++){
                    JsonObject deJsonObject = delmJsonArray.get(de).getAsJsonObject();//当前行
                    Set deSet = deJsonObject.entrySet();
                    Iterator<Map.Entry<String, JsonElement>> deIt = deSet.iterator();
                    while (deIt.hasNext()){
                        Map.Entry<String, JsonElement> deEntry = deIt.next();
                        String deKey= deEntry.getKey();
                        if (StringUtils.isNotEmpty(deKey)&&deKey.equals("top")){
                            y = Double.parseDouble(deEntry.getValue().getAsString());
                        }
                        if (StringUtils.isNotEmpty(deKey)&&deKey.equals("left")){
                            x =  Double.parseDouble(deEntry.getValue().getAsString());
                        }
                        if (StringUtils.isNotEmpty(deKey)&&deKey.equals("text")){
                            String content = deEntry.getValue().toString();
                            if (StringUtils.isNotEmpty(content))
                                content = content.replaceAll("\"","");
                            textPosition.setX(x);
                            textPosition.setY(y);
                            textPosition.setContent(content);
                            flag = true;
                            break;
                        }
                    }
                    if (flag){
                        break;
                    }
                }
            }
        }else {
            return textPosition;
        }

        return textPosition;
    }

    //后一行
    private ComponentResult.TextPosition getBehindString(JsonArray dataJsonArray,int row){
        ComponentResult.TextPosition textPosition = new ComponentResult().new TextPosition();
        Double x = 0.0;
        Double y = 0.0;
        boolean flag = false;
        if (dataJsonArray.isJsonNull())
            return textPosition;

        JsonArray delmJsonArray = null;
        if (row<=dataJsonArray.size()-2) {
            delmJsonArray = dataJsonArray.get(row + 1).getAsJsonArray();
            if (!delmJsonArray.isJsonNull()){
                for (int de=0;de<delmJsonArray.size();de++){
                    JsonObject deJsonObject = delmJsonArray.get(de).getAsJsonObject();//当前行
                    Set deSet = deJsonObject.entrySet();
                    Iterator<Map.Entry<String, JsonElement>> deIt = deSet.iterator();
                    while (deIt.hasNext()){
                        Map.Entry<String, JsonElement> deEntry = deIt.next();
                        String deKey= deEntry.getKey();

                        if (StringUtils.isNotEmpty(deKey)&&deKey.equals("top")){
                            y = Double.parseDouble(deEntry.getValue().getAsString());
                        }
                        if (StringUtils.isNotEmpty(deKey)&&deKey.equals("left")){
                            x =  Double.parseDouble(deEntry.getValue().getAsString());
                        }
                        if (StringUtils.isNotEmpty(deKey)&&deKey.equals("text")){
                            String content = deEntry.getValue().toString();
                            if (StringUtils.isNotEmpty(content))
                                content = content.replaceAll("\"","");
                            textPosition.setX(x);
                            textPosition.setY(y);
                            textPosition.setContent(content);
                            flag = true;
                            break;
                        }
                    }
                    if (flag){
                        break;
                    }
                }
            }
        }else {
            return textPosition;
        }

        return textPosition;
    }

}
